'use strict'

import Vue from 'vue'
import axios from 'axios'

const config = {
  // baseURL: 'http://localhost:8081/reagent',
  // timeout: 60 * 1000,
  // withCredentials: true,
  // changeOrigin: true
}

const _axios = axios.create(config)

//异步请求前在header里加入token
_axios.interceptors.request.use(
  config => {
    // 判断是否存在token，如果存在的话，则每个http header都加上token
    let token = sessionStorage.getItem('token')
    if (!config.headers.hasOwnProperty('token') && token) {
      config.headers.token = token
    }
    return config
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

//异步请求后，判断token是否过期
_axios.interceptors.response.use(
  response => {
    if (response.data.status === 403) {
      this.$message.error(Response.data.message)
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    } else {
      return response
    }
  },
  error => {
    return Promise.reject(error)
  }
)

Plugin.install = function (Vue, options) {
  Vue.axios = _axios
  window.axios = _axios
  Object.defineProperties(Vue.prototype, {
    axios: {
      get () {
        return _axios
      }
    },
    $axios: {
      get () {
        return _axios
      }
    }
  })
}

Vue.use(Plugin)

export default Plugin
